package com.acme.bankapp.domain.bank;

import com.acme.bankapp.exceptions.NotEnoughFundException;

import java.io.Serializable;

public abstract class AbstractAccount implements Account{
    protected double balance;
    protected int accountID;

    public AbstractAccount(double balance) throws IllegalArgumentException {
        if ( balance < 0 )
            throw new IllegalArgumentException();

        this.balance = balance;
    }

    public int getAccountID() {
        return accountID;
    }

    public void setAccountID(int accountID) {
        this.accountID = accountID;
    }

    @Override
    public void deposit(double amount) {
        balance += amount;
    }

    @Override
    public void withdraw(double amount) throws NotEnoughFundException {
        if ( amount > balance )
            throw new NotEnoughFundException();
        balance -= amount;
    }

    @Override
    public double getBalance() {
        return balance;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AbstractAccount that = (AbstractAccount) o;

        if (accountID != that.accountID) return false;
        if (Double.compare(that.balance, balance) != 0) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        temp = balance != +0.0d ? Double.doubleToLongBits(balance) : 0L;
        result = (int) (temp ^ (temp >>> 32));
        result = 31 * result + accountID;
        return result;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("AbstractAccount{")
                .append("balance=")
                .append(balance)
                .append(", accountID=")
                .append(accountID)
                .append('}');
               return sb.toString();
    }

    @Override
    public double maximumAmountToWithdraw() {
        return balance;
    }

    @Override
    public long decimalValue() {
        return Math.round(balance);
    }
}
